package com.groupfour.attendancemanagementsystem.controller;

import com.groupfour.attendancemanagementsystem.entity.DTO.PersonalInformationDTO;
import com.groupfour.attendancemanagementsystem.entity.VO.GroupRankingVO;
import com.groupfour.attendancemanagementsystem.entity.VO.PersonalGroupMemberVO;
import com.groupfour.attendancemanagementsystem.entity.VO.ProjectScoreVO;
import com.groupfour.attendancemanagementsystem.service.StudentService;
import com.groupfour.attendancemanagementsystem.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 个人信息管理
 */
@RestController
public class PersonalInformationManagementController {

    @Autowired
    private StudentService studentService;


    /**
     * 卑微小峰
     * 获取个人信息
     */
    @PostMapping("/getPersonalInformation")
    @ResponseBody
    public Result<Object> getPersonalInformation(@RequestHeader("token")String token){
        boolean expiration = JwtHelper.isExpiration(token);
        if (expiration) {
            return Result.build(null, ResultCodeEnum.TOKEN_ERROR);
        }
        //从token中解析出用户编号
        String userCode = JwtHelper.getUserCode(token);

        PersonalInformationDTO personal = studentService.getPersonalInformation(userCode);
        return Result.ok(personal);
    }

    /**
     * 更改头像
     */
    @PostMapping("/updateHeadPortrait")
    @ResponseBody
    public Result upload(MultipartFile file, String userCode) throws IOException {
        System.out.println(file+"======");
        //获取原始文件名
        String originalFilename = file.getOriginalFilename();
        //生成一个uuid名称出来
        String uuidFilename = UploadFile.getUUIDName(originalFilename);

        //产生一个随机目录
        String randomDir = UploadFile.getDir();

        File fileDir = new File("D:/uploadfiles" + randomDir);
        //若文件夹不存在,则创建出文件夹
        if (!fileDir.exists()) {
            fileDir.mkdirs();
        }
        //创建新的文件夹
        File newFile = new File("D:/uploadfiles" + randomDir, uuidFilename);
        //将文件输出到目标的文件中
        file.transferTo(newFile);
        //将保存的文件路径更新到用户信息headimg中
        String savePath = "D:/uploadfiles" +randomDir + "/" + uuidFilename;
        System.out.println(savePath);
        int succ =studentService.updateStudentPhoto(savePath,userCode);
        System.out.println(succ);
        if (succ>0){
            return Result.ok(savePath);
        }
        return Result.fail();
    }


    /**
     * 卑微小峰
     * 更改手机号码
     */
    @PostMapping("/updateStudentPhone")
    @ResponseBody
    public Result updateStudentPhone(@RequestHeader("token")String token,
                                     @RequestParam("newPhone")String newPhone) {
        boolean expiration = JwtHelper.isExpiration(token);
        if (expiration) {
            return Result.build(null, ResultCodeEnum.TOKEN_ERROR);
        }
        //从token中解析出用户编号
        String userCode = JwtHelper.getUserCode(token);

        int result = studentService.updateStudentPhone(newPhone,userCode);

        return Result.ok(result);
    }

    /**
     * 卑微小峰
     * 更改身份证号码
     */
    @PostMapping("/updateStudentIdentity")
    @ResponseBody
    public Result updateStudentIdentity(@RequestHeader("token")String token,
                                        @RequestParam("newIdentity")String newIdentity) {
        //判断token是否失效
        boolean expiration = JwtHelper.isExpiration(token);
        if (expiration) {
            return Result.build(null, ResultCodeEnum.TOKEN_ERROR);
        }
        //从token中解析出用户编号
        String userCode = JwtHelper.getUserCode(token);

        int result = studentService.updateStudentIdentity(newIdentity,userCode);
        return Result.ok(result);
    }
    /**
     * 章珑赢
     * 获取实训小组成员
     */
    @PostMapping("/SelectAllGroupMembers")
    @ResponseBody
    public Result SelectAllGroupMembers(String userCode){
//        @RequestHeader("token")String token
//        boolean expiration=JwtHelper.isExpiration(token);
//        if (expiration){
//            return Result.build(null,ResultCodeEnum.TOKEN_ERROR);
//        }
//    //        从token中解析出用户的学生学号
//        String userCode=JwtHelper.getUserCode(token);
        List<PersonalGroupMemberVO> personalGroupMemberVOS=studentService.selectGroupMember(userCode);
        return Result.ok(personalGroupMemberVOS);
    }
    /**
     * 章珑赢
     * 获取小组得分
     */
    @PostMapping("/SelectAllProjectScore")
    @ResponseBody
    public Result SelectAllProjectScore(String userCode){
//        @RequestHeader("token")String token
//        boolean expiration=JwtHelper.isExpiration(token);
//        if (expiration){
//            return Result.build(null,ResultCodeEnum.TOKEN_ERROR);
//        }
//        //        从token中解析出用户的学生学号
//        String userCode=JwtHelper.getUserCode(token);
        List<ProjectScoreVO> projectScoreVOS=studentService.SelectAllProjectScore(userCode);
        return Result.ok(projectScoreVOS);
    }

    @RequestMapping("/SelectRank")
    @ResponseBody
    public Result SelectRank(String courseCode){
        List<GroupRankingVO> groupRankingVOS=studentService.selectGroupRanking(courseCode);
        return Result.ok(groupRankingVOS);
    }
}
